<?php

/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 fdm=marker: */

/**
 * LightBot_Listener_Remote
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://framework.zend.com/license/new-bsd
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@zend.com so we can send you a copy immediately.
 *
 * @category  LightBot
 * @package   LightBot_Listener
 * @copyright Copyright (c) 2008 Olivier Hoareau <hoareau.olivier@gmail.com>
 * @author    Olivier Hoareau <hoareau.olivier@gmail.com>
 * @license   License http://framework.zend.com/license   New BSD License
 * @version   $Id: Remote.php 78 2008-06-22 13:32:54Z hoareau.olivier $
 */

require_once dirname(__FILE__) . '/../IListener.php';

// {{{ LightBot_Listener_Remote

/**
 * Remote Listener implementation
 *
 * @category  LightBot
 * @package   LightBot_Listener
 * @copyright Copyright (c) 2008 Olivier Hoareau <hoareau.olivier@gmail.com>
 * @author    Olivier Hoareau <hoareau.olivier@gmail.com>
 * @license   License http://framework.zend.com/license   New BSD License
 */
class LightBot_Listener_Remote implements LightBot_IListener
{
    // {{{ properties

    /**
     * Light bot
     * @var LightBot_ILightBot
     */
    private $lightBot;

    /**
     * Timestamp of last iteration
     *
     * @var integer
     */
    private $lastIteration;
    
    // }}}
    // {{{ __construct()

    /**
     * Constructs a new Default listener
     */
    public function __construct()
    {
        $this->lightBot = null;
    }

    // }}}
    // {{{ setLightBot()

    /**
     * Sets the light bot
     *
     * @param  LightBot_ILightBot $lightBot the light bot
     * @return LightBot_IListener $this
     */
    public function setLightBot(LightBot_ILightBot $lightBot)
    {
        $this->lightBot = $lightBot;
    }

    // }}}
    // {{{ getLightBot()

    /**
     * Returns the light bot
     *
     * @return LightBot_ILightBot the light bot
     */
    public function getLightBot()
    {
        return $this->lightBot;
    }

    // }}}
    // {{{ pushRemoteActionIfKeyExists()

    /**
     * Pushes the remote action (uri) only if an uri has been specified in the specified key
     *
     * @param  string $key              the key
     * @param  array  $params           the extra parameters (post)
     * @return LightBot_Listener_Remote $this
     */
    protected function pushRemoteActionIfKeyExists($key,$params)
    {
        $uri = $this->getLightBot()->getProperty($key,null);
        if($uri!==null){
            $this->getLightBot()->getProtocolAdapter()->pushRemoteAction($uri,$params);
        }
    }

    // }}}
    // {{{ messageReceived()

    /**
     * Notifies that a message has been received
     *
     * @param  array    $msg the message event
     */
    public function messageReceived($message)
    {
        $this->pushRemoteActionIfKeyExists('listener.message.uri',$message);
    }

    // }}}
    // {{{ presenceReceived()

    /**
     * Notifies that a presence has been received
     *
     * @param  array    $presence the presence event
     */
    public function presenceReceived($presence)
    {
        $this->pushRemoteActionIfKeyExists('listener.presence.uri',$presence);
    }

    // }}}
    // {{{ subscriptionRequestReceived()

    /**
     * Notifies that a subscription request has been received
     *
     * @param  array    $subscription the subscription event
     */
    public function subscriptionRequestReceived($subscription)
    {
        $this->pushRemoteActionIfKeyExists('listener.subscription.request.uri',$subscription);
    }

    // }}}
    // {{{ subscriptionConfirmationReceived()

    /**
     * Notifies that a subscription confirmation has been received
     *
     * @param  array    $subscription the subscription event
     */
    public function subscriptionConfirmationReceived($subscription)
    {
        $this->pushRemoteActionIfKeyExists('listener.subscription.confirmation.uri',$subscription);
    }

    // }}}
    // {{{ unsubscriptionRequestReceived()

    /**
     * Notifies that a unsubscription request has been received
     *
     * @param  array    $unsubscription the subscription event
     */
    public function unsubscriptionRequestReceived($unsubscription)
    {
        $this->pushRemoteActionIfKeyExists('listener.unsubscription.request.uri',$unsubscription);
    }

    // }}}
    // {{{ unsubscriptionConfirmationReceived()

    /**
     * Notifies that a subscription request has been received
     *
     * @param  array    $subscription the subscription event
     */
    public function unsubscriptionConfirmationReceived($subscription)
    {
        $this->pushRemoteActionIfKeyExists('listener.unsubscription.confirmation.uri',$subscription);
    }

    // }}}
    // {{{ composingReceived()

    /**
     * Notifies that a composing event has been received
     *
     * @param  array    $composing the composing event
     */
    public function composingReceived($composing)
    {
        $this->pushRemoteActionIfKeyExists('listener.composing.uri',$composing);
    }

    // }}}
    // {{{ errorReceived()

    /**
     * Notifies that an error event has been received
     *
     * @param  array    $error the error event
     */
    public function errorReceived($error)
    {
        $this->pushRemoteActionIfKeyExists('listener.error.uri',$error);
    }

    // }}}
    // {{{ contactsUpdated()

    /**
     * Notifies that a contact list update has been received
     *
     * @param  array    $contactsUpdate the contacts list update event
     */
    public function contactsUpdated($contactsUpdate)
    {
        $this->pushRemoteActionIfKeyExists('listener.contacts.update.uri',$contactsUpdate);
    }

    // }}}
    // {{{ vCardUpdated()

    /**
     * Notifies that a contact vcard update has been received
     *
     * @param array $event the contact vcard info
     */
    public function vCardUpdated($event)
    {
        $this->pushRemoteActionIfKeyExists('listener.vcard.update.uri',$event);
    }

    // }}}
    // {{{ connected()

    /**
     * Notifies that a new socket connection to jabber server is opened
     *
     * @param  array    $event the connect event
     */
    public function connected($event)
    {
        $this->pushRemoteActionIfKeyExists('listener.connected.uri',$event);
    }

    // }}}
    // {{{ disconnected()

    /**
     * Notifies that a socket has been closed
     *
     * @param  array    $event the disconnect event
     */
    public function disconnected($event)
    {
        $this->pushRemoteActionIfKeyExists('listener.disconnected.uri',$event);
    }

    // }}}
    // {{{ loggedIn()

    /**
     * Notifies of a bot login
     *
     * @param  array    $event the login event
     */
    public function loggedIn($event)
    {
        $this->pushRemoteActionIfKeyExists('listener.login.uri',$event);
    }

    // }}}
    // {{{ loggedOut()

    /**
     * Notifies of a bot logout
     *
     * @param  array    $event the logout event
     */
    public function loggedOut($event)
    {
        $this->pushRemoteActionIfKeyExists('listener.logout.uri',$event);
    }

    // }}}
    // {{{ mainLoopIterationEnd()

    /**
     * Notifies of a bot loop iteration
     *
     */
    public function mainLoopIterationEnd()
    {
        $iterationDelay = 30; // each 30s
        $curTime = time();
        if(($curTime-$this->lastIteration)>$iterationDelay){
            $this->pushRemoteActionIfKeyExists('listener.loopiteration.uri',array('bot'=>$this->getLightBot()->getProperty('login'),'time'=>$curTime));
            $this->lastIteration = $curTime;
        }
    }

    // }}}
}   

// }}}

/*
 * Local variables:
 * tab-width: 4
 * c-basic-offset: 4
 * c-hanging-comment-ender-p: nil
 * End:
 */
